package net.asfun.jvalog.control;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.asfun.jvalog.common.InteractException;
import net.asfun.jvalog.entity.Message;
import net.asfun.jvalog.service.CommentService;
import net.asfun.jvalog.util.HTMLInputFilter;
import net.asfun.jvalog.util.InputFilter;
import net.asfun.jvalog.util.RequestUtil;
import net.asfun.jvalog.util.ResponseUtil;
import net.asfun.jvalog.util.ValueChecker;
import net.asfun.jvalog.vo.Pager;
import net.asfun.jvalog.vo.Setting;
import net.asfun.jvalog.mvc.RequestMapping;

public class CommentController {
	

	@RequestMapping("/comment")
	public String index(HttpServletRequest req, Map<String, Object> model) {
		int page = RequestUtil.getIntParam(req, "page");
		Pager pager = new Pager(page);
		model.put("comments", CommentService.listAll(pager));
		model.put("pager", pager);
		
		return "comments.html";
	}
	
	@RequestMapping("/comment/delete")
	public String delete(HttpServletRequest req, Map<String, Object> model) {
		Long[] ids = RequestUtil.getLongParams(req, "id");
		for (int i=0; i<ids.length; i++) {
			CommentService.delete(ids[i]);
		}
		return ResponseUtil.feedback("comment.delete", req, model);
	}
	
	@RequestMapping("/comment/approve")
	public String approve(HttpServletRequest req, Map<String, Object> model) {
		Long[] ids = RequestUtil.getLongParams(req, "id");
		for (int i=0; i<ids.length; i++) {
			CommentService.approve(ids[i]);
		}
		return ResponseUtil.feedback("comment.approve", req, model);
	}
	
	@RequestMapping("/do/commentIt")
	public String add(HttpServletRequest req, Map<String, Object> model) {
		String author = RequestUtil.getRequiredParam(req, "author");
		author = InputFilter.length(author, 20);
		author = InputFilter.escape(author);
		String email = RequestUtil.getRequiredParam(req, "email");
		if ( ! ValueChecker.isEmail(email) ) {
			throw new InteractException("email is'nt correct.");
		}
//		String url = RequestUtil.getStringParam(req, "url");
		String comment = RequestUtil.getRequiredParam(req, "content");
		comment = trimComment(comment);
		comment = InputFilter.length(comment, 400);
		comment = new HTMLInputFilter().filter(comment);
		Long postId = RequestUtil.getLongParam(req, "postId");
		
		Message message = new Message();
		message.setContent(comment);
		message.setDate(new Date());
		message.setEmail(email);
		message.setUserName(author);
		CommentService.addComment(message, postId);
		
		if ( Setting.NEED_CHECK_COMM == 0 ) {
			model.put("action", "reload");
		}
		
		return ResponseUtil.feedback("comment.add", req, model);
	}
	
	private String trimComment(String comment) {
		String temp = comment.trim();
		if ( temp.startsWith("<br />") ) {
			return trimComment(temp.substring(6));
		}
		if ( temp.endsWith("<br />") ) {
			return trimComment(temp.substring(0, temp.length()-6));
		}
		return temp;
	}
}
